package com.tx.mq.producer;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {
    private static final String QUEUE_NAME = "test";
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory cf = new ConnectionFactory();
        cf.setHost("localhost");

        Connection connection = cf.newConnection();
            Channel channel = connection.createChannel();
            channel.exchangeDeclare("bbb", BuiltinExchangeType.TOPIC);
//            channel.queueDeclare(QUEUE_NAME, false, false, false, null);

            // 开启confirm，消息到达broker就返回确认
            channel.confirmSelect();
            channel.addReturnListener((returnMessage) -> {
                System.out.println("returns " + returnMessage.getReplyText());
            });

            channel.addConfirmListener((long deliveryTag, boolean multiple) -> {
                System.out.println("success and multiple is " + true);
            }, (long deliveryTag, boolean multiple) -> {
                System.out.println("failure and multiple is " + true);
            });

            for(int i=0;i<100;i++) {
                String message = "hello world" + i;
                // 设置mandatory标记需要稳妥传递到queue，到达则返回成功，否则失败
                channel.basicPublish("bbb", "bbb.ccc",true, null, message.getBytes());
                System.out.println("[x] Sent '" + message + "'");
            }

    }
}
